#!/usr/bin/env bash
#
##########################################################################

# Create FIFOs if needed
if [ ! -p /dev/shm/SDRcommands ]; then
mkfifo /dev/shm/SDRcommands
fi

if [ ! -p /dev/shm/SDRmeter ]; then
mkfifo /dev/shm/SDRmeter
fi

if [ ! -p /dev/shm/SDRspectrum ]; then
mkfifo /dev/shm/SDRspectrum
fi

if [ ! -p /dev/shm/hw_commands ]; then
mkfifo /dev/shm/hw_commands
fi


socat -u -b 65536 UDP-LISTEN:19002   PIPE:/dev/shm/SDRspectrum  &
socat -u          UDP-LISTEN:19003   PIPE:/dev/shm/SDRmeter &


#cd /hpsdr/trunk/OzyJanus-Jack/install
cd /sdrshell/OzyJanus-Jack/install
./initozy
sleep 5

# define SDR name 
NAME=MERC
export SDR_DEFRATE=48000
export JACK_BUFFER=512

# start jackd 
echo "starting jackd ..."

jackd -R -P70 -dalsa -dhw:0 -r$SDR_DEFRATE -p512 -n3 &

sleep 5
# start RX DttSP
echo "starting DttSP ${NAME}_RX ..."
sdr-core --spectrum --metering --client-name=${NAME}_RX --buffsize=${JACK_BUFFER} --ringmult=4 --command-port=19001 --spectrum-port=19002 --meter-port=19003  &

sleep 4
# connect receiver for Alsa
echo "connecting Mercury Alsa receiver ..."
jack_connect ozy:rx_in_l ${NAME}_RX:il
sleep 2
jack_connect ozy:rx_in_r ${NAME}_RX:ir
sleep 2
jack_connect ${NAME}_RX:ol system:playback_1
sleep 2
jack_connect ${NAME}_RX:or system:playback_2

sleep 5
echo "creating pipe 19001 for SDR Receiver"
passport /dev/shm/SDRcommands &

sleep 4

#cd /hpsdr/trunk/N6LYT/ozy/dist/Release/GNU-Linux-x86
cd /sdrshell/N6LYT/ozy/dist/Release/GNU-Linux-x86

./ozy &

sleep 4


jack_connect ozy:rx_in_l MERC_RX:il
sleep 1
jack_connect ozy:rx_in_r MERC_RX:ir
sleep 1
jack_connect ozy:mon_out_l MERC_RX:ol
sleep 1
jack_connect ozy:mon_out_r MERC_RX:or
sleep 4
echo "creating pipe 19601 for Mercury and starting sdr-shell"

socat -u  PIPE:/dev/shm/hw_commands UDP:localhost:19601 &
sleep 2
sdr-shell &



